package com.white.note.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.white.note.NoteDetailActivity;
import com.white.note.R;
import com.white.note.adapter.SearchListAdapter;
import com.white.note.base.BaseFragment;
import com.white.note.db.NoteDbHelper;
import com.white.note.entity.NoteInfo;
import com.white.note.entity.UserInfo;

import java.util.List;


public class SearchFragment extends BaseFragment {

    private EditText et_search;
    private SearchListAdapter mSearchListAdapter;
    private RecyclerView recyclerView;
    private LinearLayoutCompat ll_empty;

    @Override
    protected int getLayoutResid() {
        return R.layout.fragment_search;
    }

    @Override
    protected void initViews() {
        et_search = rootView.findViewById(R.id.et_search);
        recyclerView = rootView.findViewById(R.id.recyclerView);
        ll_empty = rootView.findViewById(R.id.ll_empty);

        // 初始化适配器
        mSearchListAdapter = new SearchListAdapter();

        // 设置适配器
        recyclerView.setAdapter(mSearchListAdapter);
        // mSearchListAdapter点击事件
        mSearchListAdapter.setOnItemClickListener(new SearchListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position, NoteInfo noteInfo) {
                Intent intent = new Intent(getActivity(), NoteDetailActivity.class);
                intent.putExtra("noteInfo", noteInfo);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void setListener() {
        rootView.findViewById(R.id.btn_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = et_search.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    Toast.makeText(getActivity(), "搜索内容不能为空~~", Toast.LENGTH_SHORT).show();
                } else {
                    List<NoteInfo> noteInfoList = NoteDbHelper.getInstance(getActivity()).searchUserNote(UserInfo.getsUserInfo().getUsername(), content);
                    if (null != mSearchListAdapter) {
                        mSearchListAdapter.setNoteInfoList(noteInfoList);
                    }
                    if (noteInfoList.size() == 0) {
                        ll_empty.setVisibility(View.VISIBLE);
                    } else {
                        ll_empty.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    @Override
    protected void initData() {

    }
}